/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__resp_data_analysis.c                            */
/*  NOTE      =                                                    */
/*  DATE      = 2017/03/24 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <cq_city_payment/cq_city_payment_core.h>
#include "frsutl.h"
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frreport_comm.h"
#include "frbyte_buffer.h"
#include "../../../src/module/cq_city_payment/config.h"

typedef struct __st_cq_city_payment_data
{
    L4_transaction st_l4_trans;                 //L4信息统计
    Frlist_header *p_report_comm;               //用于临时存储report的公共部分数据
    Frlist_header *p_report_req;                //用于临时存储report的请求部分数据
} cq_city_payment_data;

typedef struct __st_L7session_cq_city_payment_private
{
    unsigned long ul_seq;                       //序列号，1次req和response时，使用同一个序列号
    L4_transaction st_l4_trans;                 //数据
    Frreport_body_data *p_st_body_data_req;
    Frreport_body_data *p_st_body_data_resp;
    Frbyte_Packet *p_req_packet;                //存请求bytebuff数据
    Frbyte_Packet *p_resp_packet;               //存相应bytebuff数据
} L7session_cq_city_payment_private;

extern L7session_cq_city_payment_private* __cq_city_payment_private_create(void);
extern int __resp_data_analysis(Conntrack *p_st_conn
                                , Apm_packet *p_st_packet
                                , Frhash_header *p_st_hash_report
                                , L7_session *p_st_session);
extern int cq_city_payment_session_destroy(void *p_private_session);
extern void list_report_node_free(Frlist_node *p_st_node);

//全局哈希表的自旋锁
extern pthread_spinlock_t g_spin_cq_list_lock;
//哈希表头存cq_city_payment_data
extern Frhash_header *p_cq_city_payment_list;

static int __suite_init(void)
{
    pthread_spin_init(&g_spin_cq_list_lock, 0);
    p_cq_city_payment_list = frhash2_create2(CQ_HASH_SIZE);
	return 0;
}

static int __suite_cleanup(void)
{
    frhash2_free2(p_cq_city_payment_list, cq_city_payment_list_node_free);
    pthread_spin_destroy(&g_spin_cq_list_lock);
	return 0;
}

static void case1(void)
{   
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    Frhash_header *p_st_hash_report = NULL;
    L7_session *p_st_session = NULL;
    
    i_ret = __resp_data_analysis(p_st_conn, p_st_data_in, p_st_hash_report, p_st_session);
    
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    CU_ASSERT_PTR_EQUAL(p_st_session, NULL);

    return;
}

static void case2(void)
{   
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 3), FR_SUCC);
    cq_city_payment_data *p_st_report = NULL;
	Frlist_node *p_st_node = NULL;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    
    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = (char*)frmemdup((char*)"\x7b\x31\x3a\x30\x30\x30\x30\x32\x37\x33\x00\x37\x35\x32\x20\x20\x20\x20\x33\x32\x33\x36\x35\x33\x30\x31\x30\x30\x31\x35\x30\x30\x31\x36\x35\x33\x30\x39\x39\x39\x39\x39\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x4d\x73\x67\x31\x37\x30\x33\x32\x34\x31\x31\x31\x33\x30\x31\x30\x30\x31\x32\x37\x32\x30\x31\x37\x30\x33\x32\x34\x32\x30\x31\x37\x30\x33\x32\x34\x31\x31\x30\x39\x30\x31\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x39\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x7d\x7b\x32\x3a\x3a\x53\x42\x4e\x3a\x33\x32\x33\x36\x35\x33\x30\x31\x30\x30\x31\x35\x3a\x30\x35\x31\x3a\x32\x30\x31\x37\x30\x33\x32\x34\x3a\x4f\x43\x4e\x3a\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x31\x33\x37\x32\x36\x37\x34\x37\x38\x7d\x7b\x43\x3a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x7d\x45\x4e\x44", 248);
    if(p_st_data_in->p_payload == NULL)
        goto ERROR;
    p_st_data_in->ui_payload_len = 248;
    p_st_data_in->st_timestamp.tv_sec = 11;
    p_st_data_in->st_timestamp.tv_usec = 22;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __cq_city_payment_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;

    p_st_report = (cq_city_payment_data *)frmalloc(sizeof(cq_city_payment_data));
    memset(p_st_report, 0x00, sizeof(cq_city_payment_data));
    p_st_node = frlist_node_create(p_st_report);
    frhash2_addStr(p_cq_city_payment_list, "Msg17032411130100127", 20, p_st_node);

    i_ret = __resp_data_analysis(p_st_conn, p_st_data_in, &st_hash_report, p_st_session);
    
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);
    frhash2_free(&st_hash_report, list_report_node_free);
    cq_city_payment_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;
    
ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    } 
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            frfree(p_st_session->p_session_private);
        frfree(p_st_session);
    }          

    return;    
}

static void case3(void)
{   
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 3), FR_SUCC);
    cq_city_payment_data *p_st_report = NULL;
	Frlist_node *p_st_node = NULL;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    
    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = (char*)frmemdup((char*)"\x7b\x31\x3a\x30\x30\x30\x30\x32\x37\x33\x42\x37\x35\x32\x20\x20\x20\x20\x33\x32\x33\x36\x35\x33\x30\x31\x30\x30\x31\x35\x30\x30\x31\x36\x35\x33\x30\x39\x39\x39\x39\x39\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x4d\x73\x67\x31\x37\x30\x33\x32\x34\x31\x31\x31\x33\x30\x31\x30\x30\x31\x32\x37\x32\x30\x31\x37\x30\x33\x32\x34\x32\x30\x31\x37\x30\x33\x32\x34\x31\x31\x30\x39\x30\x31\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x39\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x7d\x7b\x32\x3a\x3a\x53\x42\x4e\x3a\x33\x32\x33\x36\x35\x33\x30\x31\x30\x30\x31\x35\x3a\x30\x35\x31\x3a\x32\x30\x31\x37\x30\x33\x32\x34\x3a\x4f\x43\x4e\x3a\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x31\x33\x37\x32\x36\x37\x34\x37\x38\x7d\x7b\x43\x3a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x7d\x45\x4e\x44", 248);
    if(p_st_data_in->p_payload == NULL)
        goto ERROR;
    p_st_data_in->ui_payload_len = 248;
    p_st_data_in->st_timestamp.tv_sec = 11;
    p_st_data_in->st_timestamp.tv_usec = 22;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __cq_city_payment_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;

    p_st_report = (cq_city_payment_data *)frmalloc(sizeof(cq_city_payment_data));
    memset(p_st_report, 0x00, sizeof(cq_city_payment_data));
    p_st_node = frlist_node_create(p_st_report);
    frhash2_addStr(p_cq_city_payment_list, "Msg17032411130100127", 20, p_st_node);

    i_ret = __resp_data_analysis(p_st_conn, p_st_data_in, &st_hash_report, p_st_session);
    
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);
    frhash2_free(&st_hash_report, list_report_node_free);
    cq_city_payment_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;
    
ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    } 
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            frfree(p_st_session->p_session_private);
        frfree(p_st_session);
    }          

    return;    
}

int add_suite__resp_data_analysis(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __resp_data_analysis()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}